#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 rambla.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import os
import platform, os, sys, datetime
import unittest
import setup_environ
import json
import raws_json
from raws_json.raws_service import RawsService, Feed, Query
from raws_json.meta.service import MetaService
from raws_json.rass.service import RassService
from raws_json.meta.meta import *

class SearchTest(unittest.TestCase):
    """ Test searches without the """

    @classmethod
    def setUpClass(cls):
        cls.meta_service = MetaService(username = setup_environ.TEST_USERNAME, password = setup_environ.TEST_PASSWORD, server = setup_environ.TEST_SERVER)
        cls.rass_service = RassService(username = setup_environ.TEST_USERNAME, password = setup_environ.TEST_PASSWORD, server = setup_environ.TEST_RASS_SERVER)
        # delete all existing content instances
        feed = cls.meta_service.getContentList()
        mf = Feed(feed)
        for e in mf.entries:
            cls.meta_service.deleteContent(e)
        # make sure files exist on the CDN
        status = cls.rass_service.getItemHeaderFromPath("aaa.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "aaa.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa.webm")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "aaa.webm", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_00.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "aaa_00.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_50.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "aaa_50.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("aaa_100.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "aaa_100.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("bbb.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "bbb.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("ccc.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "ccc.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("ddd.mp4")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "ddd.mp4", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("ggg.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "ggg.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("hhh.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "hhh.jpg", local_path = setup_environ.LOCAL_FILE)
        status = cls.rass_service.getItemHeaderFromPath("iii.jpg")
        if status != 200:
            cls.rass_service.postItem(dirpath = "", filename = "iii.jpg", local_path = setup_environ.LOCAL_FILE)

        # create content instances
        af1 = FileObj(path = "aaa.mp4", media_type = "video", duration = 60)
        af2 = FileObj(path = "aaa.webm", media_type = "video", duration = 60)
        af3 = FileObj(path = "aaa_00.jpg", media_type = "image")
        af4 = FileObj(path = "aaa_50.jpg", media_type = "image")
        af5 = FileObj(path = "aaa_100.jpg", media_type = "image")
        am1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Cars", lang = "en")
        am2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "Pixar", lang = "en")
        am3 = MetaObj(meta_name = "Publisher", vocab = "dc", text = "Pixar", lang = "en")
        am4 = MetaObj(meta_name = "Description", vocab = "dc", text = "Cars is a 2006 American computer-animated comedy-adventure film produced by Pixar", lang = "en")
        am5 = MetaObj(meta_name = "Audience", vocab = "dc", text = "6+")
        cls.cars_mc = MetaContent(name = "cars", tags = ["cars", "animated", "movie", "Pixar"], file_objs = [af1,af2,af3,af4,af5], meta_objs = [am1,am2,am3,am4,am5])
        cars_entry = cls.meta_service.createContent(cls.cars_mc.to_entry())

        bf1 = FileObj(path = "bbb.mp4", media_type = "video", duration = 120)
        bm1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Big Buck Bunny", lang = "en")
        bm2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "Blender institute", lang = "en")
        bm3 = MetaObj(meta_name = "Description", vocab = "dc", text = "Big Buck Bunny (code-named Peach) is a short computer animated film by the Blender Institute", lang = "en")
        bm4 = MetaObj(meta_name = "Audience", vocab = "dc", text = "6+")
        cls.big_buck_bunny_mc = MetaContent(name = "big_buck_bunny", tags = ["animated", "movie", "Blender", "python", "BigBuckBunny"], file_objs = [bf1,], meta_objs = [bm1, bm2, bm3,bm4])
        big_buck_bunny_entry = cls.meta_service.createContent(cls.big_buck_bunny_mc.to_entry())

        cf1 = FileObj(path = "ccc.mp4", media_type = "video", duration = 30)
        cm1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Cars Trailer", lang = "en")
        cm2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "Pixar", lang = "en")
        am2 = MetaObj(meta_name = "Publisher", vocab = "dc", text = "Pixar", lang = "en")
        cm3 = MetaObj(meta_name = "Description", vocab = "dc", text = "Trailer of the Cars movie", lang = "en")
        cls.cars_trailer_mc = MetaContent(name = "cars_trailer", tags = ["animated", "cars", "trailer"], file_objs = [cf1,], meta_objs = [cm1, cm2, cm3])
        cars_trailer_entry = cls.meta_service.createContent(cls.cars_trailer_mc.to_entry())

        df1 = FileObj(path = "ddd.mp4", media_type = "video", duration = 20)
        dm1 = MetaObj(meta_name = "Title", vocab = "dc", text = "Big Buck Bunny Trailer", lang = "en")
        dm2 = MetaObj(meta_name = "Creator", vocab = "dc", text = "blender", lang = "en")
        dm3 = MetaObj(meta_name = "Description", vocab = "dc", text = "Trailer of the Big Buck Bunny movie", lang = "en")
        cls.big_buck_bunny_trailer_mc = MetaContent(name = "bbb_trailer", tags = ["animated", "BigBuckBunny", "trailer"], file_objs = [df1,], meta_objs = [dm1, dm2, dm3])
        big_buck_bunny_trailer_entry = cls.meta_service.createContent(cls.big_buck_bunny_trailer_mc.to_entry())

        gf1 = FileObj(path = "ggg.jpg", media_type = "video", duration = 120)
        cls.mad_zombie_mc = MetaContent(name = "mad_zombie", tags = ["mad", "zombie", "betrail"], file_objs = [gf1,])
        mad_zombie_entry = cls.meta_service.createContent(cls.mad_zombie_mc.to_entry())

        hf1 = FileObj(path = "hhh.jpg", media_type = "video", duration = 120)
        cls.zombie_mc = MetaContent(name = "zombie", tags = ["zombie",], file_objs = [hf1,])
        zombie_entry = cls.meta_service.createContent(cls.zombie_mc.to_entry())

        f = FileObj(path = "iii.jpg", media_type = "video", duration = 120)
        cls.upper_case_zombie_mc = MetaContent(name = "upper_case_zombie", tags = ["Zombie","123456789"], file_objs = [f,])
        upper_case_zombie_entry = cls.meta_service.createContent(cls.upper_case_zombie_mc.to_entry())

    def test_search_name_exact(self):
        """ Search with name arg: ?name=zombie """
        qry = Query()
        qry["name"] = "zombie"
        feed = self.meta_service.getContentList(query = qry)
        mf = Feed(feed)
        self.assertEqual(1, len(mf.entries))
        for e in mf.entries:
            mc = MetaContent(entry = e)
            if unicode(mc.name) == unicode(self.zombie_mc.name):
                self.assertEqual(mc, self.zombie_mc)
            else:
                assertTrue("test_search_tag(): unexpected entry name" + mc.name)

    def test_search_name_multi(self):
        """ Search with name arg: ?name=*ombie """
        qry = Query()
        qry["name"] = "*ombie"
        feed = self.meta_service.getContentList(query = qry)
        mf = Feed(feed)
        self.assertEqual(3, len(mf.entries))
        for e in mf.entries:
            mc = MetaContent(entry = e)
            if unicode(mc.name) == unicode(self.upper_case_zombie_mc.name):
                self.assertEqual(mc, self.upper_case_zombie_mc)
            elif unicode(mc.name) == unicode(self.zombie_mc.name):
                self.assertEqual(mc, self.zombie_mc)
            elif unicode(mc.name) == unicode(self.mad_zombie_mc.name):
                self.assertEqual(mc, self.mad_zombie_mc)
            else:
                assertTrue("test_search_tag(): unexpected entry name" + mc.name)

    def test_search_name_single(self):
        """ Search with name arg: ?name=_z?mbie """
        qry = Query()
        qry["name"] = "_z?mbie"
        feed = self.meta_service.getContentList(query = qry)
        mf = Feed(feed)
        self.assertEqual(2, len(mf.entries))
        for e in mf.entries:
            mc = MetaContent(entry = e)
            if unicode(mc.name) == unicode(self.upper_case_zombie_mc.name):
                self.assertEqual(mc, self.upper_case_zombie_mc)
            elif unicode(mc.name) == unicode(self.mad_zombie_mc.name):
                self.assertEqual(mc, self.mad_zombie_mc)
            else:
                assertTrue("test_search_tag(): unexpected entry name" + mc.name)

    def test_search_name_no_result(self):
        """ Search with name arg: ?name=z?ombie """
        qry = Query()
        qry["name"] = "_z?ombie"
        feed = self.meta_service.getContentList(query = qry)
        mf = Feed(feed)
        self.assertEqual(0, len(mf.entries))

    def test_search_name__case_insensitive(self):
        """ Search with name arg: ?name=Z?mbie => name is not case sensitive, so should return 2 results"""
        qry = Query()
        qry["name"] = "_Z?mbie"
        feed = self.meta_service.getContentList(query = qry)
        mf = Feed(feed)
        self.assertEqual(2, len(mf.entries))
        for e in mf.entries:
            mc = MetaContent(entry = e)
            if unicode(mc.name) == unicode(self.upper_case_zombie_mc.name):
                self.assertEqual(mc, self.upper_case_zombie_mc)
            elif unicode(mc.name) == unicode(self.mad_zombie_mc.name):
                self.assertEqual(mc, self.mad_zombie_mc)
            else:
                assertTrue("test_search_tag(): unexpected entry name" + mc.name)

def suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(SearchTest))
   return suite

if __name__=='__main__':
    unittest.main()
